import networkx as nx

import matplotlib.pyplot as plt
def drawGraph(graph):
    #nx.draw(graph)
    nx.draw_spring(graph)
    plt.show()

def main():
    g = nx.pappus_graph()
    g = nx.bull_graph()
    g = nx.karate_club_graph()
    g = nx.davis_southern_women_graph()
    g = nx.florentine_families_graph()
    testcase = [nx.navigable_small_world_graph(30, 10, 10),
                nx.sedgewick_maze_graph()]
    print "Here"
    drawGraph(testcase[0])

import numpy as np
from enthought.mayavi import mlab
xyz=None
G = None
H = None
scalars = None
def draw3DGraph():
    global xyz, G, H, scalars
    H = nx.cycle_graph(50)
    
    G = nx.convert_node_labels_to_integers(H)
    pos = nx.circular_layout(H, dim=2, scale=10)
    xyz=np.array([(pos[v][0], pos[v][1], 3) for v in sorted(G)])
    #pos=nx.spring_layout(G,dim=3)
    # numpy array of x,y,z positions in sorted node order
    #xyz=np.array([pos[v] for v in sorted(G)])
    
    pos = nx.spectral_layout(H, dim=3, scale=2)
    xyz=np.array([(pos[v][0], pos[v][1], pos[v][2]) for v in sorted(G)])
    # scalar colors
    scalars=np.array(G.nodes())+5

    mlab.figure(1, bgcolor=(0, 0, 0))
    mlab.clf()

    pts = mlab.points3d(xyz[:,0], xyz[:,1], xyz[:,2],
                        scalars,
                        scale_factor=0.1,
                        scale_mode='none',
                        colormap='Blues',
                        resolution=20)

    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=0.01)
    mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
    mlab.show()

def draw3DPickerGraph():    
    global xyz, G, H, scalars


# Creating The Data
    H = nx.cycle_graph(50)
    # H = nx.grid_graph([3, 3, 3]);
    #H = nx.hypercube_graph(4)
    G = nx.convert_node_labels_to_integers(H)
    
# Layout The Graph  
    #pos = nx.circular_layout(H, dim=2, scale=10)
    #xyz=np.array([(pos[v][0], pos[v][1], 3) for v in sorted(G)])
    #pos=nx.spring_layout(G,dim=3)
    # numpy array of x,y,z positions in sorted node order
    #xyz=np.array([pos[v] for v in sorted(G)])
    
    pos = nx.spectral_layout(H, dim=3, scale=3)
    print pos
    xyz=np.array([(pos[v][0], pos[v][1], pos[v][2]) for v in pos])
    #xyz=np.array([(pos[v][0], pos[v][1], pos[v][2]) for v in sorted(G)])

# scalar colors
    scalars=np.array(G.nodes())+5


# Setup the Env
    
    figure = mlab.figure(1, bgcolor=(0, 0, 0))
    mlab.clf()
    figure.scene.disable_render = True

    pts = mlab.points3d(xyz[:,0], xyz[:,1], xyz[:,2],
                        scalars,
                        scale_factor=0.1,
                        scale_mode='none',
                        colormap='Blues',
                        resolution=20)

    pts.mlab_source.dataset.lines = np.array(G.edges())
    tube = mlab.pipeline.tube(pts, tube_radius=0.01)
    mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
    
# Add an outline to show the selected point and center it on the first
# data point.
    outline = mlab.outline(line_width=3)
    outline.outline_mode = 'cornered'
    outline.bounds = (pos[pos.keys()[0]][0]-0.1, pos[pos.keys()[0]][0]+0.1,
                      pos[pos.keys()[0]][1]-0.1, pos[pos.keys()[0]][1]+0.1,
                      pos[pos.keys()[0]][2]-0.1, pos[pos.keys()[0]][2]+0.1)    
    a = """
    outline.bounds = (pos[0][0]-0.1, pos[0][0]+0.1,
                      pos[0][1]-0.1, pos[0][1]+0.1,
                      pos[0][2]-0.1, pos[0][2]+0.1)
    """
# Every object has been created, we can reenable the rendering.
    figure.scene.disable_render = False
################################################################################

# Here, we grab the points describing the individual glyph, to figure
# out how many points are in an individual glyph.
    glyph_points = pts.glyph.glyph_source.glyph_source.output.points.to_array()

    def picker_callback(picker):
        """ Picker callback: this get called when on pick events.
        """
        if picker.actor in pts.actor.actors:
            # Find which data point corresponds to the point picked:
            # we have to account for the fact that each data point is
            # represented by a glyph with several points
            point_id = picker.point_id/glyph_points.shape[0]
            # If the no points have been selected, we have '-1'
            if point_id != -1:
                # Retrieve the coordinnates coorresponding to that data
                # point
                x, y, z = pos[point_id]
                # Move the outline to the data point.
                outline.bounds = (x-0.1, x+0.1,
                                  y-0.1, y+0.1,
                                  z-0.1, z+0.1)


    picker = figure.on_mouse_pick(picker_callback)

# Decrease the tolerance, so that we can more easily select a precise
# point.
    picker.tolerance = 0.01
    #mlab.title('Click on red balls')    
    mlab.show()


if __name__ == "__main__":
    draw3DPickerGraph()
    
